<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item label="">
          <el-input
            v-model="listQuery.name"
            placeholder="微信ID/单号"
            clearable
            @keyup.enter.native="handleFilter"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="">
          <el-date-picker
            v-model="listQuery.search_date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click.native="handleFilter"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleBatchDelete"
            >批量删除</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        :data="datas"
        border
        v-loading="listLoading"
        fit
        highlight-current-row
        style="width: 100%"
        size="small"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          align="center"
          width="40"
        ></el-table-column>
        <el-table-column
          v-for="column in tableColumns"
          :key="column.key"
          :label="column.title"
          :prop="column.key"
          :align="column.align"
          :width="column.width ? column.width : ''"
        ></el-table-column>
        <el-table-column align="center" label="操作" width="90px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="handleEdit(scope)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getPayResultList"
      />
    </el-card>
    <!-- 对话框表单 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :title="getTitle"
      width="70%"
      top="10px"
    >
      <el-form
        :model="form"
        :rules="formRules"
        ref="formRef"
        label-width="100px"
        label-position="right"
        style="width: 95%; padding: 0 30px"
        status-icon
        size="small"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="返回状态码" prop="return_code">
              <el-input
                type="text"
                v-model="form.return_code"
                placeholder="返回状态码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="返回信息" prop="return_msg">
              <el-input
                type="text"
                v-model="form.return_msg"
                placeholder="返回信息"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公众账号ID" prop="appid">
              <el-input
                type="text"
                v-model="form.appid"
                placeholder="公众账号ID"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商户号" prop="mch_id">
              <el-input v-model="form.mch_id" placeholder="商户号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备号" prop="device_info">
              <el-input v-model="form.device_info" placeholder="设备号">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="随机字符串" prop="nonce_str">
              <el-input
                v-model="form.nonce_str"
                placeholder="随机字符串"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="签名" prop="sign">
              <el-input v-model="form.sign" placeholder="签名"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签名类型" prop="sign_type">
              <el-input v-model="form.sign_type" placeholder="签名类型">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务结果" prop="result_code">
              <el-input v-model="form.result_code" placeholder="业务结果">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="错误代码" prop="err_code">
              <el-input v-model="form.err_code" placeholder="错误代码">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="错误代码描述" prop="err_code_des">
              <el-input v-model="form.err_code_des" placeholder="错误代码描述">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户标识" prop="openid">
              <el-input v-model="form.openid" placeholder="用户标识">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否关注" prop="is_subscribe">
              <el-input
                v-model="form.is_subscribe"
                placeholder="是否关注公众账号"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易类型" prop="trade_type">
              <el-input v-model="form.trade_type" placeholder="交易类型">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="付款银行" prop="bank_type">
              <el-input v-model="form.bank_type" placeholder="付款银行">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单金额" prop="total_fee">
              <el-input v-model="form.total_fee" placeholder="订单金额">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应结订单金额" prop="settlement_total_fee">
              <el-input
                v-model="form.settlement_total_fee"
                placeholder="应结订单金额"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货币种类" prop="fee_type">
              <el-input v-model="form.fee_type" placeholder="货币种类">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="现金支付金额" prop="cash_fee">
              <el-input v-model="form.cash_fee" placeholder="现金支付金额">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付货币类型" prop="cash_fee_type">
              <el-input
                v-model="form.cash_fee_type"
                placeholder="现金支付货币类型"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="总代金券金额" prop="coupon_fee">
              <el-input v-model="form.coupon_fee" placeholder="总代金券金额">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代金券使用量" prop="coupon_count">
              <el-input v-model="form.coupon_count" placeholder="代金券使用量">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="代金券类型" prop="coupon_type_0">
              <el-input v-model="form.coupon_type_0" placeholder="代金券类型">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代金券ID" prop="coupon_id_0">
              <el-input v-model="form.coupon_id_0" placeholder="代金券ID">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="微信订单号" prop="transaction_id">
              <el-input
                v-model="form.transaction_id"
                placeholder="微信支付订单号"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商户订单号" prop="out_trade_no">
              <el-input v-model="form.out_trade_no" placeholder="商户订单号">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="支付完成时间" prop="time_end">
              <el-input v-model="form.time_end" placeholder="支付完成时间">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="添加时间" prop="add_time">
              <el-input v-model="form.add_time" placeholder="添加时间">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="danger" @click="dialogVisible = false" size="mini"
          >关闭</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getPayResults, delPayResults } from "@/api/wechat";

import Pagination from "@/components/Pagination";

const defaultForm = {
  id: null,
  return_code: null,
  return_msg: null,
  appid: null,
  mch_id: null,
  device_info: null,
  nonce_str: null,
  sign: null,
  result_code: null,
  err_code: null,
  err_code_des: null,
  openid: null,
  is_subscribe: null,
  trade_type: null,
  bank_type: null,
  total_fee: null,
  settlement_total_fee: null,
  fee_type: null,
  cash_fee: null,
  cash_fee_type: null,
  coupon_fee: null,
  coupon_count: null,
  coupon_type_0: null,
  coupon_id_0: null,
  coupon_fee_0: null,
  coupon_type_1: null,
  coupon_id_1: null,
  coupon_fee_1: null,
  transaction_id: null,
  out_trade_no: null,
  attach: null,
  time_end: null,
  add_time: null,
};

export default {
  name: "PayResult",
  data() {
    return {
      form: Object.assign({}, defaultForm),
      formRules: {},
      tableColumns: [
        {
          key: "return_code",
          title: "返回状态码",
          align: "center",
        },
        {
          key: "appid",
          title: "公众账号ID",
          align: "center",
        },
        {
          key: "mch_id",
          title: "商户号",
          align: "center",
        },
        {
          key: "result_code",
          title: "业务结果",
          align: "center",
          width: 135,
        },
        {
          key: "err_code",
          title: "错误代码",
          align: "center",
          width: 90,
        },
        {
          key: "openid",
          title: "用户标识",
          align: "center",
        },
        {
          key: "is_subscribe",
          title: "是否关注",
          align: "center",
        },
        {
          key: "trade_type",
          title: "交易类型",
          align: "center",
        },
        {
          key: "total_fee",
          title: "订单金额",
          align: "center",
        },
        {
          key: "cash_fee",
          title: "现金支付",
          align: "center",
        },
        {
          key: "transaction_id",
          title: "微信订单号",
          align: "center",
        },
        {
          key: "out_trade_no",
          title: "商户订单号",
          align: "center",
        },
      ],
      title: "微信支付结果",
      dialogVisible: false,
      dlgUploadVisible: false,
      dialogType: "new",
      datas: [],
      selections: [],

      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 15,
        name: null,
        search_date: [],
      },
    };
  },
  computed: {
    ...mapGetters(["uid"]),
    getTitle() {
      return this.dialogType == "new"
        ? `新增${this.title}`
        : `查看${this.title}`;
    },
  },
  components: {
    Pagination,
  },
  created() {
    this.getPayResultList();
  },
  methods: {
    getPayResultList() {
      this.listLoading = true;
      getPayResults(this.listQuery)
        .then((res) => {
          this.datas = res.results;
          this.listLoading = false;
          this.total = res.count;
        })
        .catch((error) => {
          console.log(error);
          this.$message.error("获取数据失败!");
          this.listLoading = false;
        });
    },
    handleSelectionChange(val) {
      this.selections = val.map((item) => item.id);
      console.log(this.selections);
    },
    handleBatchDelete() {
      if (this.selections.length == 0) {
        this.$message.warning("请选择要删除记录");
        return;
      }
      this.$confirm("确定要删除选中记录吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const sel_ids = {
            select_ids: this.selections,
          };
          const res = await delPayResults(sel_ids);
          this.getPayResultList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1;
      this.getPayResultList();
    },

    // 修改
    handleEdit(scope) {
      this.form = Object.assign({}, scope.row);
      this.dialogType = "edit";

      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.formRef.clearValidate();
      });
    },
  },
};
</script>

<style lang='scss' scoped>
</style>